package com.simon.hse.fragment;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.entity.OnlineExaminOutput;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 考试
 */
@SuppressWarnings("all")
public class ExaminationFragment extends BaseFragment {
    @ViewInject(R.id.common_recyclerview)
    RecyclerView mRecyclerView;

    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<OnlineExaminOutput.DataBean.PXQuestionsListBean.PXQuestionsOptionsBean> mDataBeanList = new ArrayList<>();// 考题集合
    private Map<Integer, Boolean> answerMap = new HashMap<>();// 考题选中集合
    private String answerId = "";
    private String[] answers;// 继续答题中已答的答案

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_examination, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initRecyclerView();
        initData();
    }

    private void initRecyclerView() {
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(getContext(), R.layout.layout_item_examination, mDataBeanList);
        mCommonEmptyAdapter.setItemDatasListener(new CommonEmptyAdapter.ItemDatasListener<OnlineExaminOutput.DataBean.PXQuestionsListBean.PXQuestionsOptionsBean>() {
            @Override
            public void setItemDatas(CommonViewHolder holder, OnlineExaminOutput.DataBean.PXQuestionsListBean.PXQuestionsOptionsBean dataBean, final int position) {
                CheckBox cb_examin = holder.getView(R.id.cb_examin);
                TextView tv_examin_msg = holder.getView(R.id.tv_examin_msg);

                cb_examin.setChecked(answerMap.get(position));
                tv_examin_msg.setText(dataBean.getOptionName() + "、" + dataBean.getOptionComment());
                cb_examin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        answerMap.put(position, !answerMap.get(position));
                        StringBuffer sb = new StringBuffer();
                        for (Map.Entry<Integer, Boolean> entry : answerMap.entrySet()) {
                            if (entry.getValue()) {
                                sb.append(mDataBeanList.get(entry.getKey()).getOptionName() + "、");
                            }
                        }
                        MyApplication.getInstance().mMapExamin.put(answerId, sb.toString());
                        mCommonEmptyAdapter.notifyDataSetChanged();
                    }
                });
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                answerMap.put(position, !answerMap.get(position));
                StringBuffer sb = new StringBuffer();
                for (Map.Entry<Integer, Boolean> entry : answerMap.entrySet()) {
                    if (entry.getValue()) {
                        sb.append(mDataBeanList.get(entry.getKey()).getOptionName() + "、");
                    }
                }
                MyApplication.getInstance().mMapExamin.put(answerId, sb.toString());
                mCommonEmptyAdapter.notifyDataSetChanged();
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    private void initData() {
        Bundle bundle = getArguments();
        String examin_answer = bundle.getString("EXAMIN_ANSWER");

        OnlineExaminOutput.DataBean.PXQuestionsListBean dataBean = (OnlineExaminOutput.DataBean.PXQuestionsListBean) bundle.getSerializable("EXAMIN_ITEM");
        answerId = dataBean.getId();

        List<OnlineExaminOutput.DataBean.PXQuestionsListBean.PXQuestionsOptionsBean> px_questionsOptions = dataBean.getPX_QuestionsOptions();
        mDataBeanList.addAll(px_questionsOptions);
        for (int i = 0; i < mDataBeanList.size(); i++) {
            answerMap.put(i, false);
        }

        if (!TextUtils.isEmpty(examin_answer)){
            answers = examin_answer.split("、");
            for (int i = 0; i < mDataBeanList.size(); i++) {
                for (String answer : answers) {
                    if (answer.equals(mDataBeanList.get(i).getOptionName())){
                        answerMap.put(i, true);
                    }
                }
            }
        }

        mCommonEmptyAdapter.notifyDataSetChanged();
    }
}
